<?php
namespace App\Services;

use Illuminate\Database\Eloquent\Model;

class UrlCast extends \WeSoonNet\LaravelPlus\Casts\UrlCast
{
    /**
     * Cast the given value.
     *
     * @param  Model   $model
     * @param  string  $key
     * @param  string  $value
     * @param  array   $attributes
     *
     * @return float
     */
    public function get($model, $key, $value, $attributes)
    {
        return url($value, [], config('app.debug', true));
    }

    /**
     * Prepare the given value for storage.
     *
     * @param  Model   $model
     * @param  string  $key
     * @param  string  $value
     * @param  array   $attributes
     *
     * @return string
     */
    public function set($model, $key, $value, $attributes)
    {
        return (is_string($value) && strlen($value)) ? str_replace(url('', [], !config('app.debug', true)), '', $value) : '';
    }
}
